var HttpRequest = {
	url: '',
	ajax: null,
	async: true,
	type: 'json',
	sucCallback: this.success,
	errCallback: this.error,
	responseContent: '',
	getXMLHttpRequest: function (){
		var ajax = null;
		try{
			ajax = new XMLHttpRequest();
		} catch (e){
			try{
				ajax = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(ee){
				try{
					ajax = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(eee){
					return false;
				}
			}
		}

		this.ajax = ajax;
		return this.ajax;
	},

	typeResponse: function(objXhr){
		if (this.type == 'json'){
			if (typeof(objXhr) == 'object'){
				this.responseContent =  objXhr.responseText;
			} else {
				if (typeof(objXhr.responseTex) != 'object'){
					this.responseContent =  JSON.parse(objXhr.responseText);
				} else {
					this.responseContent =  objXhr.responseText;
				}
			}
		} else if (this.type == 'xml') {
			this.responseContent = objXhr.responseXML;
		} else {
			this.responseContent = objXhr.responseText;
		}

		return this.responseContent;
	},
	
	formatResponse: function(objXhr){
		var responseContent = '';
		if (this.type == 'json'){
			if (typeof(objXhr) == 'object'){
				this.ajax = objXhr;
				this.responseContent =  objXhr.responseText;
			} else {
				this.responseContent = JSON.parse(objXhr.responseText);
			}
		} else if (this.type == 'xml') {
			this.responseContent = objXhr.responseXML;
		} else {
			this.responseContent = objXhr.responseText;
		}

		return this.responseContent;
	},

	get: function(url, callback, errCallback){
		this.url = url || this.url;
		this.ajax = this.getXMLHttpRequest();
		this.ajax.async = true;
		this.ajax.sucCallback = callback;
		this.ajax.errCallback = errCallback || this.errCallback;
		var objAjax = this.ajax;
		var that = this;
		this.ajax.onreadystatechange = function()
  		{
  			return that.onStateChange(objAjax, that.formatResponse(objAjax));
  		}

		this.ajax.open('GET', url, this.async);
		this.ajax.send();
	},

	post: function(url, params, callback, errCallback){
		this.url = url || this.url;
		this.ajax = this.ajax || this.getXMLHttpRequest();
		this.ajax.sucCallback = callback || this.sucCallback;
		this.ajax.errCallback = errCallback || this.errCallback;
		this.ajax.async = true;
		var objAjax = this.ajax;
		var that = this;
		this.ajax.onreadystatechange = function (){
			return that.onStateChange(objAjax, that.formatResponse(objAjax));
		}
		this.ajax.open('POST', url, this.ajax.async);
		this.ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.ajax.send(params);
	},

	onStateChange: function(e, result){
		var self = e;
		var jsonResult = {};
		try{
			jsonResult = JSON.parse(self.responseText);
		}catch(e){
			jsonResult = self.responseText;
		}

		if (self.readyState == 4){
			if (self.status == 200){
				return self.sucCallback(jsonResult);
			} else if (self.status >= 300){
				return self.errCallback(self.responseText);
			}
		} else if (self.readyState == 0){
			return self.errCallback(self.responseText);
		}
	},

	success: function(ajax){
		if (ajax.readyState == 4 && ajax.status == 200){
			console.log('success: ' + ajax);
		} else {
			console.log('result: ' + ajax.responseText);	
		}
	},

	error: function(ajax){
		console.error('error: ' + ajax);
	}
};
